﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using WpfChart.DataStruct;
using Visifire.Charts;

namespace WpfChart
{
    /// <summary>
    /// CandleStockChatDemo.xaml 的交互逻辑
    /// </summary>
    public partial class CandleStockChatDemo : Window
    {
        ObservableCollection<StockData> stockObjColle = new ObservableCollection<StockData>();

        public CandleStockChatDemo()
        {
            InitializeComponent();
            MyBindChart();

            MyChart.AxesX[0].OnZoom += new EventHandler<AxisZoomEventArgs>(MyChart_OnZoom);
            MyChart2.AxesX[0].OnZoom += new EventHandler<AxisZoomEventArgs>(MyChart2_OnZoom);
            MyChart.AxesX[0].Scroll += new EventHandler<AxisScrollEventArgs>(MyChart_Scroll);
            MyChart2.AxesX[0].Scroll += new EventHandler<AxisScrollEventArgs>(MyChart2_Scroll);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        public void GetStockData()
        {
            List<StockData> lstData = new List<StockData>();
            StockData data1 = new StockData(2454, 2465, 2470, 2430, "2012-01-01",100);
            StockData data2 = new StockData(2465, 2462, 2465, 2435, "2012-02-01",99);
            StockData data3 = new StockData(2462, 2451, 2460, 2432, "2012-03-01",89);
            StockData data4 = new StockData(2451, 2461, 2472, 2432, "2012-04-01",98);
            StockData data5 = new StockData(2461, 2465, 2473, 2433, "2012-05-01",67);
            StockData data6 = new StockData(2465, 2463, 2474, 2434, "2012-06-01",78);
            StockData data7 = new StockData(2463, 2465, 2475, 2435, "2012-07-01",87);
            StockData data8 = new StockData(2455, 2467, 2476, 2436, "2012-08-01",67);
            StockData data9 = new StockData(2467, 2438, 2463, 2437, "2012-09-01",66);
            StockData data10 = new StockData(2438, 2454, 2456, 2438, "2012-10-01",65);
            StockData data11 = new StockData(2454, 2466, 2457, 2439, "2012-11-01",71);
            StockData data12 = new StockData(2466, 2445, 2447, 2423, "2012-12-01",72);
            StockData data13 = new StockData(2454, 2465, 2470, 2430, "2013-01-01",76);
            StockData data14 = new StockData(2465, 2462, 2465, 2435, "2013-02-01",73);
            StockData data15 = new StockData(2462, 2451, 2460, 2432, "2013-03-01",52);
            StockData data16 = new StockData(2451, 2461, 2472, 2432, "2013-04-01",55);
            StockData data17 = new StockData(2461, 2465, 2473, 2433, "2013-05-01",58);
            StockData data18 = new StockData(2465, 2463, 2474, 2434, "2013-06-01",43);
            StockData data19 = new StockData(2463, 2465, 2475, 2435, "2013-07-01",37);
            StockData data20 = new StockData(2455, 2467, 2476, 2436, "2013-08-01",30);
            StockData data21 = new StockData(2467, 2438, 2463, 2437, "2013-09-01",66);
            StockData data22 = new StockData(2438, 2454, 2456, 2438, "2013-10-01",68);
            StockData data23 = new StockData(2454, 2466, 2457, 2439, "2013-11-01",61);
            StockData data24 = new StockData(2466, 2445, 2447, 2423, "2013-12-01",22);
            StockData data25 = new StockData(2454, 2465, 2470, 2430, "2011-01-01",26);
            StockData data26 = new StockData(2465, 2462, 2465, 2435, "2011-02-01",29);
            StockData data27 = new StockData(2462, 2451, 2460, 2432, "2011-03-01",37);
            StockData data28 = new StockData(2451, 2461, 2472, 2432, "2011-04-01",31);
            StockData data29 = new StockData(2461, 2465, 2473, 2433, "2011-05-01",47);
            StockData data30 = new StockData(2465, 2463, 2474, 2434, "2011-06-01",49);
            StockData data31 = new StockData(2463, 2465, 2475, 2435, "2011-07-01",43);
            StockData data32 = new StockData(2455, 2467, 2476, 2436, "2011-08-01",77);
            StockData data33 = new StockData(2467, 2438, 2463, 2437, "2011-09-01",75);
            StockData data34 = new StockData(2438, 2454, 2456, 2438, "2011-10-01",90);
            StockData data35 = new StockData(2454, 2466, 2457, 2439, "2011-11-01",74);
            StockData data36 = new StockData(2466, 2466, 2467, 2423, "2011-12-01",80);

            lstData.AddRange(new StockData[] { data25,data26,data27,data28,data29,data30,
                                               data31,data32,data33,data34,data35,data36,
                                               data1, data2, data3, data4, data5, data6, data7, data8, data9, data10,
                                               data11, data12,data13,data14,data15,data16,data17,data18,data19,data20,
                                               data21,data22,data23,data24});
            foreach (StockData data in lstData)
                stockObjColle.Add(data);

            stockObjColle[0].ShowTime = " ";

        }

        public void MyBindChart()
        {
            GetStockData();


            //添加蜡烛体
            Axis axisX = new Axis();
            axisX.IntervalType = IntervalTypes.Months;
            axisX.ValueFormatString = "yyyy.MM";
            axisX.Interval = 6;
            axisX.ScrollBarSize = 0;
            MyChart.AxesX.Add(axisX);

            Axis axisY = new Axis();
            axisY.AxisMaximum = 2490;
            axisY.AxisMinimum = 2410;
            //axisY.Prefix = "$";
            MyChart.AxesY.Add(axisY);
            MyChart.Series[0].DataSource = stockObjColle;

            //添加成交量柱子
            Axis axisX2 = new Axis();
            axisX2.IntervalType = IntervalTypes.Months;
            axisX2.ValueFormatString = "yyyy.MM";
            axisX2.Interval = 6;
            axisX2.ScrollBarSize = 26;

            Axis axisY2 = new Axis();
            axisY2.AxisMaximum = 2490;
            axisY2.AxisMinimum = 2410;
            axisY2.AxisType = AxisTypes.Primary;
            //axisY.Prefix = "$";
            MyChart2.AxesX.Add(axisX2);
            MyChart2.AxesY.Add(axisY2);
            MyChart2.Series[0].DataSource = stockObjColle;

            //添加持仓线
            Axis axisY3 = new Axis();
            axisY3.AxisType = AxisTypes.Secondary;
            MyChart2.AxesY.Add(axisY3);
            MyChart2.Series[1].AxisYType = AxisTypes.Secondary;
            MyChart2.Series[1].DataSource = stockObjColle;

        }

        #region 多图拖动关联 

        void MyChart_Scroll(object sender, AxisScrollEventArgs e)
        {
            Axis axis = sender as Axis;
            MyChart2.AxesX[0].ScrollBarOffset = axis.ScrollBarOffset;
        }

        void MyChart2_Scroll(object sender, AxisScrollEventArgs e)
        {
            Axis axis = sender as Axis;
            MyChart.AxesX[0].ScrollBarOffset = axis.ScrollBarOffset;
        }

        void MyChart_OnZoom(object sender, AxisZoomEventArgs e)
        {
            Axis axis = sender as Axis;
            MyChart2.AxesX[0].Zoom(e.MinValue, e.MaxValue);
            MyChart2.AxesX[0].ScrollBarOffset = axis.ScrollBarOffset;
        }

        void MyChart2_OnZoom(object sender, AxisZoomEventArgs e)
        {
            Axis axis = sender as Axis;
            MyChart.AxesX[0].Zoom(e.MinValue, e.MaxValue);
            MyChart.AxesX[0].ScrollBarOffset = axis.ScrollBarOffset;
        }

        #endregion

        private void dataSeries_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

    }
}
